package 递归.多路递归.杨辉三角_记忆法优化;

public class Test {
    /**
     *  二维数组实现
     */
    public static int element(int[][] a, int i, int j) {
        if (a[i][j] != 0) {
            return a[i][j];
        }
        if (j == 0 || i == j) {
            a[i][j] = 1;
            return a[i][j];
        }
        a[i][j] = element(a,i - 1, j) + element(a,i - 1, j -1);
        return a[i][j];
    }

    public static void printSpace(int n, int i) {
        for (int j = 0; j < (n - i - 1) * 7; j++) {
            System.out.print(" ");
        }
    }

    public static void print(int n) {
        int[][] a = new int[n][];
        for (int i = 0; i < n; i++) {
            a[i] = new int[i + 1];
            printSpace(n, i);
            for (int j = 0; j <= i; j++) {
                System.out.printf("%-14d", element(a, i, j));
            }
            System.out.println();
        }
    }


    public static void main(String[] args) {
        print(100);
    }
}
